package com.example.bank.controller;

import com.example.bank.dto.TransactionDTO;
import com.example.bank.dto.TransactionRequestDTO;
import com.example.bank.entity.Transaction;
import com.example.bank.service.TransactionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/transactions")
public class TransactionController {
    
    @Autowired
    private TransactionService transactionService;
    
    @PostMapping
    public ResponseEntity<TransactionDTO> createTransaction(@RequestBody TransactionRequestDTO requestDTO) {
        Transaction transaction = new Transaction();
        transaction.setAccount_number(requestDTO.getAccountNumber());
        transaction.setAmount(requestDTO.getAmount());
        transaction.setDescription(requestDTO.getDescription());
        transaction.setType(requestDTO.getType());
        transaction.setStatus("完成");
        transaction.setTransactionDate(LocalDateTime.now());
        
        Transaction savedTransaction = transactionService.createTransaction(transaction);
        return new ResponseEntity<>(convertToDTO(savedTransaction), HttpStatus.CREATED);
    }
    
    @GetMapping("/{id}")
    public ResponseEntity<TransactionDTO> getTransactionById(@PathVariable Long id) {
        Transaction transaction = transactionService.getTransactionById(id);
        if (transaction != null) {
            return ResponseEntity.ok(convertToDTO(transaction));
        }
        return ResponseEntity.notFound().build();
    }
    
    @GetMapping("/account/{accountNumber}")
    public ResponseEntity<List<TransactionDTO>> getTransactionsByAccountNumber(
            @PathVariable Long accountNumber) {
        List<Transaction> transactions = transactionService.getAllTransactionsByAccountNumber(accountNumber);
        List<TransactionDTO> transactionDTOs = transactions.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
        return ResponseEntity.ok(transactionDTOs);
    }
    
    @GetMapping("/phone/{phoneNumber}/type/{type}")
    public ResponseEntity<List<TransactionDTO>> getTransactionsByPhoneAndType(
            @PathVariable String phoneNumber,
            @PathVariable String type,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date) {
        
        List<Transaction> transactions = transactionService.getTransactionsByPhoneNumberAndType(
                phoneNumber, type, date);
        
        List<TransactionDTO> transactionDTOs = transactions.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
        
        return ResponseEntity.ok(transactionDTOs);
    }
    
    private TransactionDTO convertToDTO(Transaction transaction) {
        TransactionDTO dto = new TransactionDTO();
        dto.setTransactionID(transaction.getTransactionID());
        dto.setAccountNumber(transaction.getAccount_number());
        dto.setAmount(transaction.getAmount());
        dto.setTransactionDate(transaction.getTransactionDate());
        dto.setDescription(transaction.getDescription());
        dto.setStatus(transaction.getStatus());
        dto.setType(transaction.getType());
        return dto;
    }
} 